package com.xbn.xbnmall.order.config;

import com.rabbitmq.client.Channel;
import com.xbn.xbnmall.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XBN
 * @date 2020/8/24 15:51
 * @email 78742541@qq.com
 */
/*
* 启动服务的时候自动创建队列、交换机、绑定关系等
* （MQ中没有对应的队列交换机或者绑定关系的时候才会创建）
* */
@Configuration
public class MyMQConfig {

    /*
    * 死信队列
    * */
    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> arguments=new HashMap<>();
        //死信交换机，消息死亡后转发至某个交换机
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        //路由
        arguments.put("x-dead-letter-routing-key","order.release.order");
        //消息死亡时间，单位ms
        arguments.put("x-message-ttl",60000);
        Queue queue = new Queue("order.delay.queue", true,
                false, false,arguments);

        return queue;
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue", true,
                false, false);

        return queue;
    }


    @Bean
    public Exchange orderEventExchange(){
        TopicExchange topicExchange = new TopicExchange("order-event-exchange", true, false);
        return topicExchange;
    }
    //绑定关系
    @Bean
    public Binding orderCreateOrderBinding(){
        Binding binding = new Binding("order.delay.queue",
               Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",null);
            return binding;
    }
    //绑定关系
    @Bean
    public Binding orderReleaseOrderBinding(){
        Binding binding = new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",null);
        return binding;
    }
}
